<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\ShortUrlApi;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ShortUrlApiController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ShortUrlApi(), function (Grid $grid) {
            $grid->model()->orderBy('name')->orderBy('id');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('api');
            // $grid->column('apikey');
            $grid->column('state')->switch()->sortable();
            
            $grid->enableDialogCreate();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ShortUrlApi(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('api');
            $show->field('apikey');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ShortUrlApi(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('api')->required();
            $form->text('apikey');
            $form->switch('state')->default(1);
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
